home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / RS232EXP.ZIP / RS232EXP.C next >
Text File  |  1989-07-27  |  12KB  |  410 lines

  1. /*
  2.     HEADER:        CUG236;
  3.     TITLE:        RS232 Diagnostic Helper (Kila86);
  4.     DATE:        05/17/1987;
  5.     DESCRIPTION:    "Helps diagnose problems with the RS232 serial port.";
  6.     VERSION:    1.1;
  7.     KEYWORDS:    Serial port, RS-232;
  8.     FILENAME:    RS232EXP.C;
  9.     SEE-ALSO:    RS232EXP.DOC;
  10.     COMPILERS:    vanilla;
  11.     AUTHORS:    J. Kilar, W. C. Colley III;
  12. */
  13.  
  14. /*  Portability Note:  This program was originally written for the DeSmet C
  15.     compiler.  That compiler supplies a number of quirky functions that
  16.     operate the screen on the IBM PC.  This program uses two of them:
  17.  
  18.     void scr_rowcol(r,c)        Set the cursor to row r (1-24),
  19.     int r, c;            column c (1-80).
  20.  
  21.     void scr_clr()            Clear the screen.
  22.  
  23.     Users on other systems will have to use equivalents from their C compilers'
  24.     libraries or write equivalents.
  25.  
  26.     The Eco-C88 compiler has equivalent routines (that require ANSI.SYS to be
  27.     installed) as follows:
  28.  
  29. void clrscr(), scr_pos();
  30. #define scr_rowcol(r,c)        scr_pos((r)-1,(c)-1)
  31. */
  32. #define scr_clr            clrscr
  33. #define scr_rowcol(r,c) gotoxy(c,r)
  34. /*
  35.  
  36.     Folks using serial terminals (like my Z-19) can write routines like these:
  37.  
  38. void scr_rowcol(r,c)
  39. int r, c;
  40. {
  41.     fprintf(stderr,"\033Y%c%c",r + (' '-1),c + (' '-1));
  42. }
  43.  
  44. void scr_clr()
  45. {
  46.     fprintf(stderr,"\033Y  \033E");
  47. }
  48.  
  49.     You can also work directly into the ANSI.SYS driver or work into an ANSI
  50.     serial terminal like this:
  51.  
  52. void scr_rowcol(r,c)
  53. int r, c;
  54. {
  55.     fprintf(stderr,"\033[%d;%dH",r,c);
  56. }
  57.  
  58. void scr_clr()
  59. {
  60.     fprintf(stderr,"\033[2J");
  61. }
  62.  
  63. */
  64. #include <stdio.h>
  65.  
  66. /*  Portability Note:  A few compilers don't know the additional type
  67.     void.  If yours is one of these, uncomment the following #define.    */
  68.  
  69. /* #define    void        int                    */
  70.  
  71. int baudrate(), bothpc(), bothterm(), chkcable(), databits(), dcedte();
  72. int decterm(), deviceoff(), fullcable(), parity(), pc(), pcmale();
  73. int pcmodem(), pcport(), pcprint(), pcterm(), resp(), respny(), term();
  74. void blank(), clear(), devincomp(), heading(), nullmodem(), wait();
  75.  
  76. void main()
  77. {
  78.     while (1) {
  79.     heading();            /* display heading */
  80.     if (deviceoff()) break;        /* check that devices are on */
  81.     if (chkcable()) break;        /* check cable connected */
  82.     if (baudrate()) break;        /* check baud rate */
  83.     if (parity()) break;        /* parity */
  84.     if (databits()) break;        /* check data bits */
  85.     if (pc()) {            /* handle PC involved case */
  86.         if (pcmale()) break;    /* on serial not parallel port? */
  87.         if (pcport()) break;    /* COM1 or COM2 */
  88.         if (bothpc()) break;    /* two PCs case */
  89.         if (pcterm()) break;    /* PC to a terminal case */
  90.         if (pcmodem()) break;    /* PC to a modem case */
  91.         if (pcprint()) break;    /* PC to a printer */
  92.         if (fullcable()) break;    /* try full straight thru cable */
  93.         devincomp();  break;
  94.     }
  95.     else if (term()) {        /* no PC, terminal case */
  96.         if (pcmodem()) break;
  97.         if (decterm()) break;    /* DEC terminal */
  98.         if (bothterm()) break;    /* both terminals */
  99.         if (fullcable()) break;    /* try 25 lead straight thru cable */
  100.         if (dcedte()) break;
  101.         devincomp();  break;
  102.     }
  103.     else {
  104.         if (dcedte()) break;
  105.         devincomp();  break;
  106.     }
  107.     }
  108.     blank();  blank();
  109.     printf("Glad I could be of service to you.\n");
  110. }
  111.  
  112. /* heading - displays header for program */
  113. void heading()
  114. {
  115.     clear();
  116.     printf("RS-232 MINI EXPERT SYSTEM.\n");
  117.     printf("Version 1.1   by   Joe Kilar\n");
  118.     blank();
  119.     printf("This program will help you solve RS-232 serial communications ");
  120.     printf("problems.\n");
  121.     printf("Examples are connecting PCs to serial printers, modems, or");
  122.     printf(" terminals.\n");
  123.     blank();
  124.     printf("If you're not sure about an answer you may try skipping it by\n");
  125.     printf("answering no to whether the devices are communicating.  ");
  126.     printf("Jot down\n");
  127.     printf("the question in case further aids don't result in success.\n");
  128.     wait();
  129.     return;
  130. }
  131.  
  132.  
  133. int deviceoff()
  134. {
  135.     clear();
  136.     printf("Check that power to both devices is on.  If power is not \n");
  137.     printf("on, turn it on. \n");
  138.     return respny();
  139. }
  140.  
  141.  
  142. int chkcable()
  143. {
  144.     clear();
  145.     printf("Check that the serial cable is firmly connected to both ");
  146.     printf("devices.\nIf not, ensure a good connection. \n");
  147.     return respny();
  148. }
  149.  
  150. int baudrate()
  151. {
  152.     clear();
  153.     printf("Check that the baud rates selected or required for both devices");
  154.     printf("\nare the same.   Baud rate reflects the speed at which data is");
  155.     printf("\ncommunicated.  Typical baud rates are 300,1200,2400,9600, and ");
  156.     printf("19.2K\n");
  157.     blank();
  158.     printf("Some devices have auto-baud.  They lock onto the baud rate of\n");
  159.     printf("the other device sending successive RETURNs (sometimes SPACEs). ");
  160.     printf("At times\nnoise ");
  161.     printf("may make it lock onto the wrong baud rate.  Try turning it\n");
  162.     printf("off and then back on to reset it.\n");
  163.     blank();
  164.     printf("Ensure that the baud rates are identical and/or reset any\n");
  165.     printf("device using auto-baud.\n");
  166.     return respny();
  167. }
  168.  
  169. int parity()
  170. {
  171.     clear();
  172.     printf("Check that the parity's selected or required for both devices\n");
  173.     printf("are the same.   Parity has to do with an extra bit sent that can");
  174.     printf("\nhelp detect transmission errors.  It is usually set to even, ");
  175.     printf("odd\nor none.\n");
  176.     blank();
  177.     printf("Ensure that the parity's are identical.\n");
  178.     return respny();
  179. }
  180.  
  181. int databits()
  182. {
  183.     clear();
  184.     printf("Check that each device is set for the same number of data bits.");
  185.     printf("\nUsual values are  7  or  8.\n");
  186.     blank();
  187.     printf("Ensure that the data bits selected are identical. \n");
  188.     return respny();
  189. }
  190.  
  191. int pc()
  192. {
  193.     clear();
  194.     printf("Is one or both devices an IBM or compatible PC? \n");
  195.     return resp("No", "Yes");
  196. }
  197.  
  198. int pcmale()
  199. {
  200.     clear();
  201.     printf("Check that the cable is connected to the PC's serial and\n");
  202.     printf("not parallel port.\n");
  203.     printf("The serial port has male pins protruding from it, the parallel\n");
  204.     printf("port is female. \n");
  205.     blank();
  206.     printf("Make sure the cable is connected to the serial port.\n");
  207.     return respny();
  208. }
  209.  
  210. int pcport()
  211. {
  212.     clear();
  213.     printf("If you have more than one serial port or have a PCjr, make sure");
  214.     printf("\nyou are connected to the correct serial port.\n");
  215.     blank();
  216.     printf("The port that comes on an XT, usually closest to the side with\n");
  217.     printf("the power switch, is COM1.  The serial port on back of a PCjr\n");
  218.     printf("may look like COM1 or COM2 depending on the way the software\n");
  219.     printf("accesses it.  Try switching to the other port if you\n");
  220.     printf("are in doubt.\n");
  221.     blank();
  222.     printf("Double check which port is used, switch the cable, or tell the\n");
  223.     printf("program to use the other if there is another or you have a PCjr.");
  224.     printf("\n");
  225.     return respny();
  226. }
  227.  
  228. int bothpc()
  229. {
  230.     int r;
  231.  
  232.     clear();
  233.     printf("Are both devices IBM or compatible PCs?\n");
  234.     if (r = resp("No","Yes")) nullmodem();
  235.     return r;
  236. }
  237.  
  238. int pcterm()
  239. {
  240.     int r;
  241.  
  242.     clear();
  243.     printf("Is the other device a terminal?\n");
  244.     if (r = resp("No","Yes")) nullmodem();
  245.     return r;
  246. }
  247.  
  248. int pcmodem()
  249. {
  250.     int r;
  251.  
  252.     clear();
  253.     printf("Is the other device a modem?\n");
  254.     if (r = resp("No","Yes")) (void)fullcable();
  255.     return r;
  256. }
  257.  
  258. int pcprint()
  259. {
  260.     int r;
  261.  
  262.     clear();
  263.     printf("Is the other device a printer?\n");
  264.     if (r = resp("No","Yes")) {
  265.     clear();
  266.     blank();
  267.     printf("Make sure the printer is an RS-232 device and not a ");
  268.     printf("Centronics\nparallel device.  If it is Centronics, you need ");
  269.     printf("to attach it\nto the parallel port of the PC.\n");
  270.     wait();
  271.     }
  272.     return r;
  273. }
  274.  
  275. int fullcable()
  276. {
  277.     clear();  blank();
  278.     printf("Try a straight thru cable that uses all 25 pins.  Make sure the");
  279.     printf("\ncable is not a null modem cable.\n");
  280.     return respny();
  281. }
  282.  
  283. void nullmodem()
  284. {
  285.     clear();
  286.     printf("You need a null modem cable.  You can purchase one from a\n");
  287.     printf("computer store.  You can also make one yourself by wiring\n");
  288.     printf("up a cable as shown below.\n");
  289.     blank();
  290.     printf("     2 ------------------------------------------------ 3\n");
  291.     printf("     3 ------------------------------------------------ 2\n");
  292.     printf("     4 ------------------------------------------------ 5\n");
  293.     printf("     5 ------------------------------------------------ 4\n");
  294.     printf("     6 ------------------------------------------------ 20\n");
  295.     printf("     7 ------------------------------------------------ 7\n");
  296.     printf("    20 ------------------------------------------------ 6\n");
  297.     blank();
  298.     printf("If the null modem cable doesn't work, see someone in Digital\n");
  299.     printf("Electronics for help.\n");
  300.     wait();
  301.     return;
  302. }
  303.  
  304. int term()
  305. {
  306.     clear();
  307.     printf("Is one or both devices a terminal?\n");
  308.     return resp("No","Yes");
  309. }
  310.  
  311. int decterm()
  312. {
  313.     clear();
  314.     printf("If the terminal is a DEC model such as VT100 or VT220\n");
  315.     printf("and other device is not a DEC computer or modem to a DEC\n");
  316.     printf("computer (e.g. VAX), turn off the XON/XOFF feature.\n");
  317.     return respny();
  318. }
  319.  
  320. int bothterm()
  321. {
  322.     int r;
  323.  
  324.     clear();
  325.     printf("Are both devices terminals?\n");
  326.     if (r = resp("No","Yes")) nullmodem();
  327.     return r;
  328. }
  329.  
  330. int dcedte()
  331. {
  332.     clear();
  333.     printf("RS-232 devices fall into two classes - DTE (Data Terminal ");
  334.     printf("Equip.)\nand DCE (Data Communications Equip.).  Try a full 25 ");
  335.     printf("pin straight\nthrough cable if one device is DTE and the other ");
  336.     printf("DCE.  If both\ndevices are DTEs or DCEs, you need a null modem ");
  337.     printf("cable. The device\nmanual may indicate the device's type.\n");
  338.     blank();
  339.     printf("Devices that are usually DTEs: terminals and IBM compatible PCs.");
  340.     printf("\nDevices that are usually DCEs: modems, ports to minicomputers,");
  341.     printf("\n   printers, VME bus serial ports, and devices designed to ");
  342.     printf("talk\n   with terminals such as serial ports on micro ");
  343.     printf("development\n   systems.\n");
  344.     blank();
  345.     printf("Does it seem that you need a null modem cable as the devices are");
  346.     printf("\nboth DTEs or DCEs?\n");
  347.     if (resp("No","Yes")) nullmodem();
  348.     clear();
  349.     return respny();
  350. }
  351.  
  352. void devincomp()
  353. {
  354.     clear();
  355.     printf("The devices are probably not supplying all the signals required");
  356.     printf("\nby the other.  See someone in Digital Electronics for help.\n");
  357.     wait();
  358.     return;
  359. }
  360.  
  361.  
  362. int respny()
  363. {
  364.     blank();
  365.     printf("    Are the devices communicating properly now?\n");
  366.     return resp("No","Yes");
  367. }
  368.  
  369.  
  370. int resp(zero,one)
  371. char *zero,*one;
  372. {
  373.     char buf[256], *gets();
  374.  
  375.     blank();  blank();
  376.     printf("            0  - %s \n",zero);
  377.     blank();
  378.     printf("            1 -  %s \n",one);
  379.  
  380.     do {
  381.     scr_rowcol(22,10);
  382.     printf("Enter either 0 or 1, then hit <return>: ");
  383.     } while (*gets(buf) != '0' && *buf != '1');
  384.     return *buf == '1';
  385. }
  386.  
  387. void wait()
  388. {
  389.     char buf[256], *gets();
  390.  
  391.     scr_rowcol(22,10);
  392.     printf("Hit <return> to continue.....");
  393.     (void)gets(buf);
  394.     return;
  395. }
  396.  
  397.  
  398. void clear()
  399. {
  400.     scr_clr();    scr_rowcol(1,1);
  401.     return;
  402. }
  403.  
  404. void blank()
  405. {
  406.     printf("\n");
  407.     return;
  408. }
  409.  
  410.